/**
 * 成绩管理主程序
 * 演示两个班级合并进行综合测评
 */
public class GradeManagementMain {
    public static void main(String[] args) {
        System.out.println("============= 奖学金评定系统 =============\n");

        // 定义奖学金比例
        double firstPrizeRatio = 0.10; // 一等奖10%
        double secondPrizeRatio = 0.15; // 二等奖15%
        double thirdPrizeRatio = 0.20; // 三等奖20%

        // 选择使用链表实现（也可以选择顺序表实现：ArrayGradeManagement）
        System.out.println("使用链表实现成绩管理\n");

        // ========== 创建班级1 ==========
        GradeManagementInterface class1 = new LinkedGradeManagement();
        System.out.println("========== 班级1：计算机科学与技术1班 ==========");

        // 添加班级1的学生（假设有5门课程：高等数学、大学英语、数据结构、操作系统、计算机网络）
        class1.add(new Student("2021001", "张三", "计科1班",
                new double[] { 95.0, 88.0, 92.0, 90.0, 87.0 }));
        class1.add(new Student("2021002", "李四", "计科1班",
                new double[] { 85.0, 90.0, 88.0, 85.0, 90.0 }));
        class1.add(new Student("2021003", "王五", "计科1班",
                new double[] { 92.0, 85.0, 95.0, 88.0, 91.0 }));
        class1.add(new Student("2021004", "赵六", "计科1班",
                new double[] { 78.0, 82.0, 80.0, 85.0, 83.0 }));
        class1.add(new Student("2021005", "孙七", "计科1班",
                new double[] { 88.0, 92.0, 90.0, 87.0, 89.0 }));
        class1.add(new Student("2021006", "周八", "计科1班",
                new double[] { 75.0, 78.0, 76.0, 80.0, 77.0 }));
        class1.add(new Student("2021007", "吴九", "计科1班",
                new double[] { 90.0, 88.0, 93.0, 91.0, 89.0 }));
        class1.add(new Student("2021008", "郑十", "计科1班",
                new double[] { 82.0, 85.0, 84.0, 87.0, 86.0 }));

        System.out.println("班级1人数: " + class1.size());

        // 评定班级1的奖学金
        class1.evaluateScholarship(firstPrizeRatio, secondPrizeRatio, thirdPrizeRatio);
        System.out.println("\n--- 班级1奖学金评定结果 ---");
        class1.displayAll();
        System.out.println();
        class1.displayScholarshipWinners();
        System.out.println();

        // ========== 创建班级2 ==========
        GradeManagementInterface class2 = new LinkedGradeManagement();
        System.out.println("\n========== 班级2：计算机科学与技术2班 ==========");

        // 添加班级2的学生
        class2.add(new Student("2022001", "钱一", "计科2班",
                new double[] { 93.0, 90.0, 94.0, 91.0, 92.0 }));
        class2.add(new Student("2022002", "钱二", "计科2班",
                new double[] { 87.0, 89.0, 86.0, 88.0, 90.0 }));
        class2.add(new Student("2022003", "钱三", "计科2班",
                new double[] { 91.0, 93.0, 89.0, 92.0, 91.0 }));
        class2.add(new Student("2022004", "钱四", "计科2班",
                new double[] { 80.0, 83.0, 82.0, 84.0, 81.0 }));
        class2.add(new Student("2022005", "钱五", "计科2班",
                new double[] { 89.0, 91.0, 88.0, 90.0, 89.0 }));
        class2.add(new Student("2022006", "钱六", "计科2班",
                new double[] { 77.0, 79.0, 78.0, 81.0, 80.0 }));
        class2.add(new Student("2022007", "钱七", "计科2班",
                new double[] { 94.0, 92.0, 96.0, 94.0, 93.0 }));
        class2.add(new Student("2022008", "钱八", "计科2班",
                new double[] { 83.0, 86.0, 85.0, 87.0, 84.0 }));
        class2.add(new Student("2022009", "钱九", "计科2班",
                new double[] { 86.0, 88.0, 87.0, 89.0, 88.0 }));

        System.out.println("班级2人数: " + class2.size());

        // 评定班级2的奖学金
        class2.evaluateScholarship(firstPrizeRatio, secondPrizeRatio, thirdPrizeRatio);
        System.out.println("\n--- 班级2奖学金评定结果 ---");
        class2.displayAll();
        System.out.println();
        class2.displayScholarshipWinners();
        System.out.println();

        // ========== 两个班级合并 ==========
        System.out.println("\n========== 两个班级合并综合测评 ==========");
        GradeManagementInterface mergedClass = new LinkedGradeManagement();

        // 合并两个班级
        mergedClass.merge(class1);
        mergedClass.merge(class2);

        System.out.println("合并后总人数: " + mergedClass.size());
        System.out.println("合并前班级1人数: " + class1.size());
        System.out.println("合并前班级2人数: " + class2.size());

        // 对合并后的班级重新评定奖学金
        mergedClass.evaluateScholarship(firstPrizeRatio, secondPrizeRatio, thirdPrizeRatio);

        System.out.println("\n--- 合并后综合测评结果 ---");
        mergedClass.displayAll();
        System.out.println();
        mergedClass.displayScholarshipWinners();
        System.out.println();

        // ========== 班级间成绩对比分析 ==========
        System.out.println("\n========== 班级间成绩对比分析 ==========");

        // 计算班级1的平均成绩
        double class1AvgScore = calculateAverageScore(class1);
        System.out.println("班级1平均综合成绩: " + String.format("%.2f", class1AvgScore));

        // 计算班级2的平均成绩
        double class2AvgScore = calculateAverageScore(class2);
        System.out.println("班级2平均综合成绩: " + String.format("%.2f", class2AvgScore));

        // 计算合并后的平均成绩
        double mergedAvgScore = calculateAverageScore(mergedClass);
        System.out.println("合并后平均综合成绩: " + String.format("%.2f", mergedAvgScore));

        // 对比分析
        System.out.println("\n--- 对比分析 ---");
        if (class1AvgScore > class2AvgScore) {
            System.out.println("班级1的平均成绩高于班级2，高出: " +
                    String.format("%.2f", (class1AvgScore - class2AvgScore)) + "分");
        } else if (class2AvgScore > class1AvgScore) {
            System.out.println("班级2的平均成绩高于班级1，高出: " +
                    String.format("%.2f", (class2AvgScore - class1AvgScore)) + "分");
        } else {
            System.out.println("两个班级的平均成绩相同");
        }

        // 统计各等级奖学金人数
        System.out.println("\n--- 合并后奖学金统计 ---");
        int firstPrizeCount = countScholarship(mergedClass, "一等奖");
        int secondPrizeCount = countScholarship(mergedClass, "二等奖");
        int thirdPrizeCount = countScholarship(mergedClass, "三等奖");
        System.out.println("一等奖人数: " + firstPrizeCount);
        System.out.println("二等奖人数: " + secondPrizeCount);
        System.out.println("三等奖人数: " + thirdPrizeCount);

        // 统计各班级获奖情况
        System.out.println("\n--- 各班级获奖情况统计 ---");
        countScholarshipByClass(mergedClass, "计科1班");
        countScholarshipByClass(mergedClass, "计科2班");

        System.out.println("\n============= 程序结束 =============");
    }

    /**
     * 计算班级的平均综合成绩
     */
    private static double calculateAverageScore(GradeManagementInterface gradeManagement) {
        if (gradeManagement.isEmpty()) {
            return 0.0;
        }
        double sum = 0.0;
        int count = gradeManagement.size();
        for (int i = 0; i < count; i++) {
            sum += gradeManagement.get(i).getComprehensiveScore();
        }
        return sum / count;
    }

    /**
     * 统计指定奖学金等级的人数
     */
    private static int countScholarship(GradeManagementInterface gradeManagement, String scholarship) {
        int count = 0;
        int size = gradeManagement.size();
        for (int i = 0; i < size; i++) {
            if (gradeManagement.get(i).getScholarship().equals(scholarship)) {
                count++;
            }
        }
        return count;
    }

    /**
     * 统计指定班级的获奖情况
     */
    private static void countScholarshipByClass(GradeManagementInterface gradeManagement, String className) {
        int first = 0, second = 0, third = 0;
        int size = gradeManagement.size();
        for (int i = 0; i < size; i++) {
            Student s = gradeManagement.get(i);
            if (s.getClassName().equals(className)) {
                String scholarship = s.getScholarship();
                if (scholarship.equals("一等奖")) {
                    first++;
                } else if (scholarship.equals("二等奖")) {
                    second++;
                } else if (scholarship.equals("三等奖")) {
                    third++;
                }
            }
        }
        System.out.println(className + "获奖情况 - 一等奖: " + first +
                "人, 二等奖: " + second + "人, 三等奖: " + third + "人");
    }
}

